﻿using Appliaction.Dto.Sinv;
using Appliaction.IAppServices.Sinv;
using AutoMapper;
using Domain.Entitys.Sinv;
using Domain.IRepositorys.Sinv;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Appliaction.AppServices.Sinv
{
    public class Sinv_Sale_OrderService : ISinv_Sale_OrderService
    {
        private readonly ISinv_Sale_OrderRepository sinv_Sale_OrderRepository;
        private readonly IMapper mapper;

        public Sinv_Sale_OrderService(ISinv_Sale_OrderRepository sinv_Sale_OrderRepository,IMapper mapper)
        {
            this.sinv_Sale_OrderRepository = sinv_Sale_OrderRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 查询销售订单及其详情
        /// </summary>
        /// <param name="currendPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="customerName"></param>
        /// <param name="orderCode"></param>
        /// <returns></returns>
        public List<Sinv_Sale_Order_Info> GetSinv_Sale_Infos(int currendPage, int pageSize, string customerName, string orderCode, string proName, int orderStatus, char auditStatus)
        {
            return sinv_Sale_OrderRepository.GetSinv_Sale_Infos(currendPage,pageSize,customerName,orderCode,proName,orderStatus,auditStatus);
        }

        /// <summary>
        /// 根据客户名称，销售订单名称、产品名称查询汇总
        /// </summary>
        /// <param name="customerName"></param>
        /// <param name="saleOrserName"></param>
        /// <param name="proName"></param>
        /// <returns></returns>
        public List<Sinv_Sale_Order_Info> GetSinv_Sale_InfosSummary(string customerName, string saleOrserName, string proName)
        {
            return sinv_Sale_OrderRepository.GetSinv_Sale_InfosSummary(customerName, saleOrserName, proName);
        }

        /// <summary>
        /// 根据销售订单编号查询对应销售订单信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<Sinv_Sale_Order> GetSinv_Sale_OrderByCodeAsync(string code)
        {
            return await sinv_Sale_OrderRepository.GetSinv_Sale_OrderByCodeAsync(code);
        }

        /// <summary>
        /// 分页条件查询销售订单明细
        /// </summary>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="code"></param>
        /// <param name="productCode"></param>
        /// <param name="measureId"></param>
        /// <returns></returns>
        public Task<List<Sinv_Sale_Order_Details>> GetSinv_Sale_Order_DetailsAsync(int currentPage, int pageSize, string code, string productCode, int measureId)
        {
            return sinv_Sale_OrderRepository.GetSinv_Sale_Order_DetailsAsync(currentPage,pageSize,code,productCode,measureId);
        }

        /// <summary>
        /// 根据销售订单编号查询销售明细
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<List<Sinv_Sale_Order_Details>> GetSinv_Sale_Order_DetailsByCodeAsync(string code)
        {
            return await sinv_Sale_OrderRepository.GetSinv_Sale_Order_DetailsByCodeAsync (code);
        }

        /// <summary>
        /// 条件查询销售订单总数
        /// </summary>
        /// <param name="code"></param>
        /// <param name="productCode"></param>
        /// <param name="measureId"></param>
        /// <returns></returns>
        public Task<int> GetSinv_Sale_Order_DetailsCountAsync(string code, string productCode, int measureId)
        {
            return sinv_Sale_OrderRepository.GetSinv_Sale_Order_DetailsCountAsync (code,productCode,measureId);
        }

        /// <summary>
        /// 添加销售订单信息和批量添加销售订单明细
        /// </summary>
        /// <param name="saleOrderWithDetailsDto"></param>
        /// <returns></returns>
        public Task<int> Sinv_Sale_OrderAddAsync(SaleOrderWithDetailsDto<Sinv_Sale_OrderAddDto> saleOrderWithDetailsDto)
        {
            Sinv_Sale_Order sinv_Sale_Order = mapper.Map<Sinv_Sale_Order>(saleOrderWithDetailsDto.Sinv_Sale_OrderDto);
            List<Sinv_Sale_Order_Details> sinv_Sale_Order_Details = mapper.Map<List<Sinv_Sale_Order_Details>>(saleOrderWithDetailsDto.Sinv_Sale_Order_DetailsAddDtos);
            return sinv_Sale_OrderRepository.Sinv_Sale_OrderAddAsync(sinv_Sale_Order,sinv_Sale_Order_Details);
        }

        /// <summary>
        /// 修改销售订单审核状态
        /// </summary>
        /// <param name="_Sale_OrderAuditUpdateDto"></param>
        /// <returns></returns>
        public Task<int> Sinv_Sale_OrderAuditUpdateAsync(Sinv_Sale_OrderAuditUpdateDto _Sale_OrderAuditUpdateDto)
        {
            Sinv_Sale_Order sinv_Sale_Order = mapper.Map<Sinv_Sale_Order>(_Sale_OrderAuditUpdateDto);
            return sinv_Sale_OrderRepository.Sinv_Sale_OrderAuditUpdateAsync(sinv_Sale_Order);
        }

        /// <summary>
        /// 查询销售订单总数
        /// </summary>
        /// <param name="customerName"></param>
        /// <param name="orderCode"></param>
        /// <returns></returns>
        public async Task<int> Sinv_Sale_OrderCOuntAsync(string customerName, string orderCode, string proName, int orderStatus, char auditStatus)
        {
            return await sinv_Sale_OrderRepository.Sinv_Sale_OrderCOuntAsync(customerName, orderCode,proName,orderStatus,auditStatus);
        }

        /// <summary>
        /// 逻辑删除销售订单信息
        /// </summary>
        /// <param name="sinv_Sale_OrderDelDto"></param>
        /// <returns></returns>
        public async Task<int> Sinv_Sale_OrderDelAsync(Sinv_Sale_OrderDelDto sinv_Sale_OrderDelDto)
        {
            Sinv_Sale_Order sinv_Sale_Order = mapper.Map<Sinv_Sale_Order>(sinv_Sale_OrderDelDto);
            return await sinv_Sale_OrderRepository.Sinv_Sale_OrderDelAsync(sinv_Sale_Order);
        }

        /// <summary>
        /// 销售订单撤销
        /// </summary>
        /// <param name="sinv_Sale_Order"></param>
        /// <returns></returns>
        public async Task<int> Sinv_Sale_OrderRevokeAsync(Sinv_Sale_OrderRevokeDto sinv_Sale_Order)
        {
            Sinv_Sale_Order sinv_Sale_Order1 = mapper.Map<Sinv_Sale_Order>(sinv_Sale_Order);
            return await sinv_Sale_OrderRepository.Sinv_Sale_OrderRevokeAsync(sinv_Sale_Order1);
        }

        /// <summary>
        /// 分页条件查询销售订单总数
        /// </summary>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderCode"></param>
        /// <param name="status"></param>
        /// <param name="customerName"></param>
        /// <param name="rep_id"></param>
        /// <param name="dept_id"></param>
        /// <returns></returns>
        public async Task<List<Sinv_Sale_Order>> Sinv_Sale_OrderSelectAsync(int currentPage, int pageSize, string orderCode, int status, string customerName, int rep_id, int dept_id)
        {
            return await sinv_Sale_OrderRepository.Sinv_Sale_OrderSelectAsync(currentPage,pageSize,orderCode,status,customerName,rep_id,dept_id);
        }

        /// <summary>
        /// 条件查询销售订单总数
        /// </summary>
        /// <param name="orderCode"></param>
        /// <param name="status"></param>
        /// <param name="customerName"></param>
        /// <param name="rep_id"></param>
        /// <param name="dept_id"></param>
        /// <returns></returns>
        public async Task<int> Sinv_Sale_OrderSelectCountAsync(string orderCode, int status, string customerName, int rep_id, int dept_id)
        {
            return await sinv_Sale_OrderRepository.Sinv_Sale_OrderSelectCountAsync(orderCode,status,customerName,rep_id,dept_id);
        }

        /// <summary>
        /// 分页条件查询出库销售订单信息
        /// </summary>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderCode"></param>
        /// <param name="status"></param>
        /// <param name="customerName"></param>
        /// <param name="rep_id"></param>
        /// <param name="dept_id"></param>
        /// <returns></returns>
        public async Task<List<Sinv_Sale_Order>> Sinv_Sale_OrderSelectFillAsync(int currentPage, int pageSize, string orderCode, int status, string customerName, int rep_id, int dept_id)
        {
            return await sinv_Sale_OrderRepository.Sinv_Sale_OrderSelectFillAsync(currentPage,pageSize,orderCode,status,customerName,rep_id,dept_id);
        }

        /// <summary>
        /// 条件查询出库销售订单总数
        /// </summary>
        /// <param name="orderCode"></param>
        /// <param name="status"></param>
        /// <param name="customerName"></param>
        /// <param name="rep_id"></param>
        /// <param name="dept_id"></param>
        /// <returns></returns>
        public async Task<int> Sinv_Sale_OrderSelectFillCountAsync(string orderCode, int status, string customerName, int rep_id, int dept_id)
        {
            return await sinv_Sale_OrderRepository.Sinv_Sale_OrderSelectFillCountAsync(orderCode, status, customerName, rep_id, dept_id);
        }

        /// <summary>
        /// 修改销售订单及其明细
        /// </summary>
        /// <param name="saleOrderWithDetailsDto"></param>
        /// <returns></returns>
        public async Task<int> Sinv_Sale_OrderUpdateAsync(SaleOrderWithDetailsDto<Sinv_Sale_OrderUpdateDto> saleOrderWithDetailsDto)
        {
            Sinv_Sale_Order sinv_Sale_Order = mapper.Map<Sinv_Sale_Order>(saleOrderWithDetailsDto.Sinv_Sale_OrderDto);
            List<Sinv_Sale_Order_Details> sinv_Sale_Order_Details = mapper.Map<List<Sinv_Sale_Order_Details>>(saleOrderWithDetailsDto.Sinv_Sale_Order_DetailsAddDtos);
            return await sinv_Sale_OrderRepository.Sinv_Sale_OrderUpdateAsync(sinv_Sale_Order,sinv_Sale_Order_Details);
        }
    }
}
